﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Xml;
using Microsoft.Xna.Framework.Media;

// -----------------------------------------------------------------------
// <copyright file="$safeitemrootname$.cs" company="$registeredorganization$">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace MenuGame2DXNA
{
    public class sCredits : Scene
    {
        private SpriteFont Font1;
        private Vector2 LifePos;
        private List<String> ListString;
        private List<Vector2> ListPos;
        private List<Color> ListColor;
        private List<int> ListColorFlag;
        private Dialog dialogContent;
        private Game game;
  
       
     
        private Texture2D backGround;
        int Time = 0;
        int Distance = 350;
        bool Start = false;
        public override void LoadContent(Game _game)
        {
            game = _game;
            base.LoadContent(game);
            Font1 = game.Content.Load<SpriteFont>("fontNormal");
           
            backGround = game.Content.Load<Texture2D>("Menu/background");
            ListString = new List<String>();
            ListPos = new List<Vector2>();
            ListColor = new List<Color>();
            ListColorFlag = new List<int>();
            isPlayMusic = false;
           //--------------end ------------
          
            // Load content in xml file
             XmlDocument CreditsXml = new XmlDocument();
            CreditsXml.Load("Credits.xml");
            XmlElement root = (XmlElement)CreditsXml.SelectSingleNode("Credits");
            foreach (XmlElement xe in root.ChildNodes)
            {
                XmlElement textxml = (XmlElement)xe.SelectSingleNode("text");
                XmlElement Colorxml = (XmlElement)xe.SelectSingleNode("Color");

                float[] color = new float[4];
                ListString.Add(textxml.InnerText);
                color[0] = float.Parse(Colorxml.GetAttribute("r"));
                color[1] = float.Parse(Colorxml.GetAttribute("g"));
                color[2] = float.Parse(Colorxml.GetAttribute("b"));
                color[3]= float.Parse(Colorxml.GetAttribute("a"));
                ListColorFlag.Add(3);
                for (int i = 0; i < 3; i++)
                {
                    
                    if (color[i] == 0)
                        {
                            ListColorFlag[ListColorFlag.Count - 1]=i;
                            break;
                        }
                   
                    
                }
            }
            for (int i = 0; i < ListString.Count; i++)
            {
                ListPos.Add(new Vector2(180, 480));
                ListColor.Add(new Color(0, 0, 0, 0));
            }

            dialogContent = new Dialog(game, 600, 450, null,"Credits");
            dialogContent.Appear(54);
            
        }
        private void Reset()
        {
            for (int i = 0; i < ListString.Count; i++)
            {
                ListPos[i]=new Vector2(180, 480);
                ListColor[i] = new Color(0, 0, 0, 0);
            }
            Time = 0;
        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (!isPlayMusic)
            {
                MusicManager.Play("Title");
                isPlayMusic = true;
            }
            KeyboardState kbState = Keyboard.GetState();
            if (kbState.IsKeyDown(Keys.Escape))
            {
                game.Scene = 0;
                Stop();
            }
            
            
            dialogContent.Update(gameTime, Color.White);
            if (dialogContent.State == Dialog.DialogState.ENDAPPEAR)
            {

            
                    
                    Time++;
                    if (Time % 2 == 0)
                {


                    int index = Time / 40;
                    if (index > ListString.Count)
                    {
                        index = ListString.Count;
                    }

                    for (int i = 0; i < index; i++)
                    {
                        ListPos[i] = new Vector2(ListPos[i].X, ListPos[i].Y - 2);
                        if (450 - ListPos[i].Y < Distance - 35)
                            ListColor[i] = new Color(ListColorFlag[i] != 0 ? ListColor[i].R + 10 : 0, ListColorFlag[i] != 1 ? ListColor[i].G + 10 : 0, ListColorFlag[i] != 2 ? ListColor[i].B + 10 : 0, ListColor[i].A + 10);
                        else
                            ListColor[i] = new Color(ListColorFlag[i] != 0 ? ListColor[i].R - 10 : 0, ListColorFlag[i] != 1 ? ListColor[i].G - 10 : 0, ListColorFlag[i] != 2 ? ListColor[i].B - 10 : 0, ListColor[i].A - 10);
                    }
                }
                if (ListPos[ListString.Count - 1].Y < 450 - Distance)
                    Reset();
            }
            
            
            
        }


        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {


            base.Draw(gameTime, spriteBatch);

            dialogContent.Draw(gameTime,spriteBatch);
           
            
            for (int i = 0; i < ListString.Count; i++)
                spriteBatch.DrawString(Font1, ListString[i], ListPos[i], ListColor[i]);
            
         
            

        }

       
        public override void Play()
        {

            
            dialogContent.Appear(54);
           
            Reset();
            base.Play();
        }
        public override void Stop()
        {
           
            dialogContent.DisAppear(1);
            base.Stop();
        }

    }
}
